﻿using System;
using System.Linq;
using System.Windows.Forms;
using TVRageDAL;

namespace RED
{
    public partial class AddShowForm : Form
    {
        readonly TVDAL _dal = new TVDAL();
        ShowInformation[] _shows;
        EpisodeInformation[] _episodes;

        public EpisodeInformation SelectedEpisode { get; set; }
        public ShowInformation SelectedShow { get; set; }

        public AddShowForm()
        {
            InitializeComponent();
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            UseWaitCursor = true;
            panel1.Visible = true;
            Update();

            if (showSearchBox.Text != "")
            {
                showsList.Items.Clear();
                _shows = _dal.SearchShows(showSearchBox.Text);

                if (_shows != null)
                {
                    foreach (var show in _shows)
                    {
                        showsList.Items.Add(show.Name);
                    }
                }
            }

            panel1.Visible = false;
            UseWaitCursor = false;
            Update();
        }

        private void showsList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(sender.ToString()) || showsList.SelectedItem == null) return;

            UseWaitCursor = true;
            panel1.Visible = true;
            Update();

            foreach (var show in _shows.Where(show => show.Name == showsList.SelectedItem.ToString()))
            {
                SelectedShow = show;
                wb.Navigate(show.ShowUrl);
            }

            _episodes = _dal.GetShowEpisodes(SelectedShow.ShowId);
            episodesTree.Nodes.Clear();

            if (_episodes != null && _episodes.Length > 0)
            {
                foreach (var ep in _episodes)
                {
                    if (ep == null) continue;

                    var tnc = new TreeNode { Text = ep.AirDate };

                    var tn = new TreeNode
                                 {
                                     Text = @"[" + ep.Season + @"x" + ep.Episode + @"] " + ep.Name,
                                     Name = ep.Name
                                 };

                    tn.Nodes.Add(tnc);

                    episodesTree.Nodes.Add(tn);
                }
            }

            panel1.Visible = false;
            UseWaitCursor = false;
            Update();
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.None;

            if (episodesTree.SelectedNode != null)
            {
                foreach (var ep in _episodes)
                {
                    if (ep != null && ep.Name == episodesTree.SelectedNode.Name)
                    {
                        SelectedEpisode = ep;
                        DialogResult = DialogResult.OK;

                        if (isHD.Checked)
                            SelectedEpisode.IsHd = true;
                    }
                }
            }

            Close();
        }
    }
}
